﻿
Public Class FormMain
    Implements Widgets.IXboxInfoRetrevalView
    Implements Widgets.ITwitterUpdateView

    Private m_ExitCall As Boolean = False
    Private m_LastXboxLiveData As DMXIProxy.XboxInfo = Nothing

    Private WithEvents XboxInfoRetreval As New Widgets.XboxInfoRetreval(Of FormMain)(Me)
    Private WithEvents TwitterUpdate As New Widgets.TwitterUpdate(Of FormMain)(Me)

    Public Property LastUpdateValue() As String Implements Widgets.IXboxInfoRetrevalView.LastUpdateValue
        Get
            Return My.Settings.LastUpdateValue
        End Get
        Set(ByVal value As String)
            My.Settings.LastUpdateValue = value
            My.Settings.Save()
        End Set
    End Property

    Private Sub XboxInfoRetreval_Update(ByVal r As DMXIProxy.XboxInfo) Handles XboxInfoRetreval.Update
        m_LastXboxLiveData = r
        Dim tipTitle As String = ""
        Dim tipContent As String = ""
        If r.PresenceInfo.Online Then
            tipTitle = "You are Online"
            tipContent = r.PresenceInfo.Info
        Else
            tipTitle = "You are Offline"
            tipContent = r.PresenceInfo.Info
        End If
        TwitterUpdate.Send(r.PresenceInfo.Info)
        Me.ux_NotifyIcon.ShowBalloonTip(2000, tipTitle, tipContent, ToolTipIcon.None)
    End Sub

#Region " Form Events "

    Private Sub ux_ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_ExitToolStripMenuItem.Click
        m_ExitCall = True
        Me.Close()
    End Sub

    Private Sub FormMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If Not e.CloseReason = CloseReason.WindowsShutDown Or Not m_ExitCall Then
            e.Cancel = True
        End If
    End Sub

    Private Sub ux_CheckNowToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_CheckNowToolStripMenuItem.Click
        XboxInfoRetreval.CheckXboxLiveStatus()
    End Sub

    Private Sub ux_DisplayLiveDataToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_DisplayLiveDataToolStripMenuItem.Click
        If m_LastXboxLiveData Is Nothing Then
            MsgBox("You must wait untill the data has been retrieved at least once!")
            Exit Sub
        End If
        FormDisplay.ShowXboxLiveStatus(m_LastXboxLiveData)
    End Sub

    Private Sub ux_SettingsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ux_SettingsToolStripMenuItem.Click
        GetSettings()
    End Sub

    Private Sub FormMain_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If CheckSettings() Then
            XboxInfoRetreval.CheckXboxLiveStatus()
            ResetSettings()
        End If
    End Sub

#End Region

    Private Sub ResetSettings()
        Dim timespan As New TimeSpan(0, My.Settings.MinutesBetweenChecks, 0)
        If My.Settings.Enabled And CheckSettings() Then
            XboxInfoRetreval.Start()
        Else
            XboxInfoRetreval.Stop()
        End If
    End Sub

    Public Function GetGamerTag() As String Implements Widgets.IXboxInfoRetrevalView.GetGamerTag
        If Not My.Settings.GamerTag.Length > 3 Then
            GetSettings()
        End If
        Return My.Settings.GamerTag
    End Function

    Public Function GetTwiterCredentials() As Object Implements Widgets.ITwitterUpdateView.GetTwiterCredentials
        If Not My.Settings.TwitterEmail.Length > 3 Then
            GetSettings()
        End If
        If Not My.Settings.TwitterPassword.Length > 3 Then
            GetSettings()
        End If
        If Not My.Settings.TwitterUsername.Length > 3 Then
            GetSettings()
        End If
        Return New With { _
                    .Email = My.Settings.TwitterEmail, _
                    .Password = My.Settings.TwitterPassword, _
                    .Username = My.Settings.TwitterUsername _
                        }
    End Function

    Private Sub GetSettings()
        Dim frm As New FormSettings
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            ResetSettings()
        End If
    End Sub

    Private Function CheckSettings() As Boolean
        Dim r As Boolean = True
        If My.Settings.GamerTag = "" Then
            r = False
        End If
        If My.Settings.TwitterEmail = "" Then
            r = False
        End If
        If My.Settings.TwitterPassword = "" Then
            r = False
        End If
        If My.Settings.TwitterUsername = "" Then
            r = False
        End If
        If r = False Then Me.ux_NotifyIcon.ShowBalloonTip(10000, My.Application.Info.Title, "You must enter your settings before your Twitter can be updated!", ToolTipIcon.Warning)
        Return r
    End Function



End Class
